#include "Operator.h"
#include "ui_Operator.h"
#include "c/pwm.h"
#include "CApi.h"
#include <math.h>
#include <QDebug>
#include "CApi.h"
#include <qmath.h>
#include "mainwindow.h"
Operator::Operator(QWidget *parent) :
    MyDialog(parent),
    ui(new Ui::Operator)
{
    ui->setupUi(this);
    dialogName=MyDialog::OperationDialog;
    blurEffect = new QGraphicsBlurEffect;
    blurEffect->setBlurRadius(30); // 设置模糊半径，值越大，模糊效果越强
    chart = new QtCharts::QChart();
    series = new QtCharts::QSplineSeries();
    collectLock=new QMutex;
    this->ui->chart->setChart(chart);
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->axisX()->setRange(1, xNumbersMax);
    chart->axisY()->setRange(0, 3.3);
    chart->axisX()->setTitleText("时间/ms");
    chart->axisY()->setTitleText("电压/V");
    chart->setTitle("数据采集实时曲线");
    chart->setTitleFont(QFont("Times", 30, QFont::Bold));


    collect=new Collect(this->series,collectLock,this->chart,xNumbersMax);
    connect(this->collect,&Collect::paintingChart,this,&Operator::paintingChart);
    connect(this,&Operator::isStartCollect,this->collect,&Collect::isStartCollect);
    //paintChart();
//    {//测试
//        this->series->clear();
//        qreal t = 0, y1;
//        int icount = 50;
//            for (int i = 0; i < icount; ++i)
//            {
//                y1 = qSin(i);
//                series->append(i,y1+1.3);
//            }
//    }
}

Collect *Operator::getCollect()
{
    return this->collect;
}

Operator::~Operator()
{
    delete ui;
}

void Operator::siriWork(MyDialogName name,functionFlag flag)
{

        switch (flag) {
        case functionFlag::pump_start:
            if(this->ui->collectPush->text()=="开始采集")
            {
                this->ui->mode->setText("自动模式");
                this->setFuzzyEnabled(true);
                this->on_collectPush_clicked();
            }
            break;
        case functionFlag::pump_stop:
            if(this->ui->collectPush->text()=="停止采集")
                this->on_collectPush_clicked();
            break;
        case functionFlag::call_yes:
            emit this->showPatient_signal(MyDialog::showPatientInfor);
            break;
        case functionFlag::call_cancel:
            if(name!=this->dialogName)
                this->showPatient_signal("");
            else if(name==this->dialogName)
            {
                this->showPatient_signal("");
                emit this->siriWork_signal(name,flag);
            }
            break;
        default:
            emit this->siriWork_signal(name,flag);
        }

}

void Operator::on_number1_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"1");
}

void Operator::on_number2_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"2");
}

void Operator::on_number3_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"3");
}

void Operator::on_number4_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"4");
}

void Operator::on_number5_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"5");
}

void Operator::on_number6_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"6");
}

void Operator::on_number7_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"7");
}

void Operator::on_number8_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"8");
}

void Operator::on_number9_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"9");
}

void Operator::on_number0_clicked()
{
    ui->currentHzEdit->setStyleSheet("color: red;");
    QString valueHz=this->ui->currentHzEdit->text();
    if(ui->currentHzEdit->toolTip()=="yes")
        valueHz="";
    ui->currentHzEdit->setToolTip("no");
    if(valueHz=="0")
        valueHz="";
    ui->currentHzEdit->setText(valueHz+"0");
}

void Operator::on_cancel_clicked()
{
    ui->currentHzEdit->setToolTip("no");
    ui->currentHzEdit->setStyleSheet("color: red;");
    ui->currentHzEdit->setText("0");
}

void Operator::on_yes_clicked()
{
    ui->currentHzEdit->setToolTip("yes");
    ui->currentHzEdit->setStyleSheet("color: rgb(115, 210, 22);");
    if(this->ui->collectPush->text()=="停止采集")
        beng_control(functionFlag::pump_start,1000000000/this->ui->currentHzEdit->text().toInt());
}

void Operator::on_mode_clicked()
{
    this->ui->mode->setText(this->ui->mode->text()=="手动模式"?"自动模式":"手动模式");
    if(this->ui->mode->text()=="自动模式")
        setFuzzyEnabled(true);
    else
        setFuzzyEnabled(false);
}


void Operator::setFuzzyEnabled(bool r)
{
    this->ui->widget_Hz->setEnabled(!r);
//    if(r==true)
//    {
//        this->ui->widget_Hz->setGraphicsEffect(blurEffect); // 将模糊效果应用到窗口上
//    }
   // else
    //   this->ui->widget_Hz->setGraphicsEffect(nullptr);
}

void Operator::paintChart()
{
//    QTimer *timer = new QTimer(this);
//    qDebug()<<"series"<<series<<endl;
//    connect(timer, &QTimer::timeout, [=]() {
//        static int counter = 1;
//        series->append(counter, qrand() % 3);
//        counter += 1;
//        qDebug()<<"series---------------"<<endl;
//        if(series->count()>xNumbers)
//            series->removePoints(0,1);
//       this->updateAxisRange();
//    });
//    timer->start(1000);
}


void Operator::on_collectPush_clicked()
{
    this->collect->setIsCloudAndLocalSave(this->ui->saveCloudBox->isChecked(),this->ui->saveLocalBox->isChecked());
    this->ui->collectPush->setText(this->ui->collectPush->text()=="开始采集"?"停止采集":"开始采集");
//    if(this->ui->collectPush->text()=="开始采集")
//    {
//        if(this->ui->currentHzEdit->toolTip()=="yes")
//            this->ui->collectPush->setText("停止采集");
//        else {
//            //弹窗-没有设置参数
//        }
//    }
//    else
//        this->ui->collectPush->setText("开始采集");

    if(this->ui->mode->text()=="手动模式")
    {qDebug()<<"------AutomaticMode"<<endl;
        this->collect->setIsAutomaticMode(false);
        if(this->ui->collectPush->text()=="停止采集")
        {qDebug()<<"collectPush signal"<<endl;
            this->ui->mode->setEnabled(false);
            this->ui->saveCloudBox->setEnabled(false);
            this->ui->saveLocalBox->setEnabled(false);
            emit this->isStartCollect(this->ui->collectInterval->text().toInt(),true);
            beng_control(functionFlag::pump_start,1000000000/this->ui->currentHzEdit->text().toInt());
        }
        else if(this->ui->collectPush->text()=="开始采集")
        {MainWindow::mySqlite->updataOperatorNumber();
            this->ui->mode->setEnabled(true);
            this->ui->saveCloudBox->setEnabled(true);
            this->ui->saveLocalBox->setEnabled(true);
            emit this->isStartCollect(this->ui->collectInterval->text().toInt(),false);
            beng_control(functionFlag::pump_stop,0);
        }
    }
    else if(this->ui->mode->text()=="自动模式")
    {
        qDebug()<<"------noAutomaticMode"<<endl;
        this->collect->setIsAutomaticMode(true);
        if(this->ui->collectPush->text()=="停止采集")
        {qDebug()<<"collectPush signal"<<endl;

            this->ui->mode->setEnabled(false);
            this->ui->saveCloudBox->setEnabled(false);
            this->ui->saveLocalBox->setEnabled(false);
            emit this->isStartCollect(this->ui->collectInterval->text().toInt(),true);
            beng_control(functionFlag::pump_start,1000000000/500);
        }
        else if(this->ui->collectPush->text()=="开始采集")
        {MainWindow::mySqlite->updataOperatorNumber();
            this->ui->mode->setEnabled(true);
            this->ui->saveCloudBox->setEnabled(true);
            this->ui->saveLocalBox->setEnabled(true);
            emit this->isStartCollect(this->ui->collectInterval->text().toInt(),false);
            beng_control(functionFlag::pump_stop,0);
        }
    }
}

void Operator::paintingChart()
{

    this->updateAxisRange();
}

void Operator::updateAxisRange()
{
    // 获取横轴范围
    qreal minX = std::numeric_limits<qreal>::max();
    qreal maxX = std::numeric_limits<qreal>::min();
    foreach (QtCharts::QAbstractSeries *series, chart->series()) {
        QtCharts::QXYSeries *xySeries = static_cast<QtCharts::QXYSeries*>(series);
        QPointF p1 = xySeries->at(0);
        QPointF p2 = xySeries->at(xySeries->count() - 1);

        if (p1.x() < minX) {
            minX = p1.x();
        }
        if (p2.x() > maxX) {
            maxX = p2.x();
        }
    }
    // 更新横轴范围
    chart->axisX()->setRange(minX, maxX);
}

void Operator::on_clearData_clicked()
{
    QMutexLocker lock(this->collectLock);
    this->series->clear();
    chart->axisX()->setRange(1, xNumbersMax);
    this->collect->clearData();
}
